#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/4/11 10:28
# @Author  : Grayson Liu
# @Email   : graysonliu@foxmail.com
# @File    : MethodList.py
import json


class MethodList:
    def __init__(self):
        # 初始化的项目信息
        self.project_data = {
            "name": "",
            "path": "",
            "input": {},
            "preprocess": {
                "nan_process": "none",
                "anomaly_process": "none"
            },
            "method_num": 0,
            "methods": [
            ],
            "output": {}
        }

    def set_project_name(self, project_name: str):
        self.project_data["name"] = project_name

    def set_project_path(self, project_path):
        self.project_data["path"] = project_path

    def add_method(self, method_name: str, type_index: int, method_index: int, parameters: list):
        """
        添加一个方法
        :param method_name: 方法中文名
        :param type_index: 方法类别序号
        :param method_index: 类别内方法序号
        :param parameters: 方法参数列表
        :return:
        """
        self.project_data["method_num"] += 1  # 方法数量+1
        method = {
            "name": method_name,
            "type_index": type_index,
            "method_index": method_index,
            "parameters": parameters
        }
        self.project_data["methods"].append(method)

    def delete_method(self, index: int):
        """
        删除一个方法
        :param index:
        :return:
        """
        self.project_data["method_num"] -= 1  # 方法数量-1
        self.project_data["methods"].pop(index)  # 从列表methods中删除指定序号的方法

    def set_input(self, graphic_path: str, graphic_type: str, input_raster: str):
        """
        设置方法的输入
        :param graphic_path: 遥感影像的路径
        :param graphic_type: 遥感影像的类型
        :param input_raster: 输入方式的字符串
        :return:
        """
        input_dic = {
            "path": graphic_path,
            "type": graphic_type,
            "raster": input_raster
        }
        self.project_data["input"] = input_dic

    def set_preprocess(self, nan_process: str, anomaly_process: str):
        """
        设置预处理方法
        :param nan_process: 对缺失值的处理
        :param anomaly_process: 对异常值的处理
        :return:
        """
        preprocess_dic = {
            "nan_process": nan_process,
            "anomaly_process": anomaly_process
        }
        self.project_data["preprocess"] = preprocess_dic

    def set_output(self, output_path: str, output_type: str):
        """
        设置方法的输出
        :param output_path: 输出文件的路径
        :param output_type: 输出文件的类型
        :return:
        """
        output_dic = {
            "path": output_path,
            "type": output_type
        }
        self.project_data["output"] = output_dic

    def load_project(self, path):
        """
        从项目工程文件.prj中读取信息，保存到当前对象中
        :return:
        """
        self.project_data = json.load(open(path, "r", encoding="utf-8"))

    def save_project(self, path):
        """
        保存当前项目信息到文本文件中
        :return:
        """
        json.dump(self.project_data, open(path, "w", encoding="utf-8"), ensure_ascii=False, indent=4)
